package com.lzs.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.DictType;
import com.lzs.system.mapper.DictTypeMapper;
import com.lzs.system.service.DictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author ymw
 * @since 2020-11-30
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {

    @Autowired
    private  DictTypeMapper dictTypeMapper;
    /**
     *
     * @param page
     * @param limit
     * @param dictType
     * 字典分页查询方法
     * @return
     */
    @Override
    public Page<DictType> selectAllDictTypePage(Integer page, Integer limit, DictType dictType) {
        Page<DictType> dictTypePage=new Page<>(page,limit);
        String name = dictType.getName();
        String systemFlag = dictType.getSystemFlag();
        String status = dictType.getStatus();
        QueryWrapper<DictType> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (StringUtils.isNotEmpty(systemFlag)){
            queryWrapper.eq("system_flag",systemFlag);
        }
        if (StringUtils.isNotEmpty(status)){
            queryWrapper.eq("status",status);
        }
         dictTypeMapper.selectPage(dictTypePage, queryWrapper);
        return dictTypePage;
    }

    /**
     * 新增修改字典方法
     * @param dictType
     * @return
     */
    @Override
    public R insertOrUpdateDictType(DictType dictType) {
        if (dictType.getDictTypeId()!=null){
            dictType.setUpdateTime(new Date());
            int i = dictTypeMapper.updateById(dictType);
            if (i>0){
                return R.ok().message("修改成功");
            }
            return R.ok().message("修改失败");
        }else {
            dictType.setCreateTime(new Date());
            int i = dictTypeMapper.insert(dictType);
            if (i>0){
                return R.ok().message("新增成功");
            }
            return R.ok().message("新增失败");
        }
    }

    /**
     * 新增回显内容
     * @param dictTypeId
     * @return
     */
    @Override
    public R findByDictTypeId(Long dictTypeId) {
        DictType dictType = dictTypeMapper.selectById(dictTypeId);
        return R.ok().data("dictType",dictType);
    }
}
